<? 
	require_once('conf/conf.php'); 
	require_once('includes/googleApi.php');
?>

<script type="text/javascript" src="res/js/popuplib.js"></script>
<script type="text/javascript">

	var custom_login_success_callback = null;

	$(document).ready(function() {
		_navRefresh();
	});

	var opener = popupManager.createPopupOpener({
			'redirect_uri' : '<? echo $google_app_client['redirect_uri'] ?>',    
			'onCloseHandler' : _navRefreshAfterLogin,   
			'shouldEncodeUrls' : true,
			'session_id' : '<? echo session_id(); ?>',
			'google_client' : {
				'id' : '<? echo $google_app_client['id'] ?>',
				'scope' : '<? echo $google_app_client['scope'] ?>'
			}
		});

	function doLogin() {
		opener.popup(450, 500);
		return false;
	}

	function doLogout() {
		call('ajax/mech.php?act=logout',
			function(data) {
				$('#ajax_error_message').html(ajaxErrorMessage('good bye'));
				$('#logoutin').html( __noLogin() );
			},
			function (e) {
				$('#ajax_error_message').html(ajaxErrorMessage(e.err));
			}
		);
	}

	function __noLogin() {
		return '<a class="white" href="javascript:void()" onclick="doLogin();">login</a>';
	}

	function __yesLogin(name) {
		return '<span class="white">logged in as <span class="c1">' + name + '</span> <span class="separator"> | </span><a class="white" href="javascript:void()" onclick="doLogout();">logout</a></span>';
	}

	function _navRefreshAfterLogin() {
		_navRefresh(custom_login_success_callback);
	}

	function _navRefresh(success_callback) {

		/* refresh login information */
		call('ajax/mech.php?act=qls',
			function(data) {
				if (empty(data.data.uid)) {
					$('#logoutin').html( __noLogin() );
				}
				else
				{
					$('#logoutin').html( __yesLogin(data.data.me.screen_name) );
					if (success_callback) {
						success_callback();
					}
				}
			},
			function (e) {
				$('#logoutin').html( __noLogin() );
				$('#ajax_error_message').html(ajaxErrorMessage(e.err));
			}
		);

	}

</script>

<?php
function _nav_menu($me, $cnt) {
	if ($me != $cnt)
		echo '<a href="' . $cnt . '.php" class="c1">' . $cnt . '</a>';
	else
		echo $cnt;
}
?>

<div id="nav" class="left">
<?
	if ($me != 'hidden') {
?>
		<span class="white">query: <? _nav_menu($me, 'console'); ?> | <? _nav_menu($me, 'tank'); ?> | <? _nav_menu($me, 'track'); ?></span>
<?	
	}
?>
</div>
<div class="right">
	<div id="logoutin">
	</div>
</div>